<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class favorite extends core {

	public function __construct() {
		parent::__construct();
	}
	
	//收藏总数
	public function total($item_id=0, $type_id=0) {
		$isReturn = false;
		if ($item_id) $isReturn = true;
		$item_id = (isset($_POST['item_id']) && trim($_POST['item_id'])) ? intval($_POST['item_id']) : $item_id;
		$type_id = (isset($_POST['type_id']) && trim($_POST['type_id'])) ? intval($_POST['type_id']) : $type_id;
		$count = 0;
		if ($this->member_id && $item_id) {
			$sql = "SELECT count(*) FROM {$this->tbp}favorite WHERE item_id='{$item_id}' AND type_id='{$type_id}' AND member_id='{$this->member_id}'";
			$count = intval($this->db->get_var($sql));
		}
		if ($isReturn) {
			return $count;
		} else {
			success($count);
		}
	}

	//添加、删除收藏
	public function add() {
		$item_id = (isset($_POST['item_id']) && trim($_POST['item_id'])) ? intval($_POST['item_id']) : 0;
		$type_id = (isset($_POST['type_id']) && trim($_POST['type_id'])) ? intval($_POST['type_id']) : 0;
		if ($this->member_id && $item_id) {
			$sql = "SELECT count(*) FROM {$this->tbp}favorite WHERE item_id='{$item_id}' AND type_id='{$type_id}' AND member_id='{$this->member_id}'";
			$exists = intval($this->db->get_var($sql));
			if ($exists) {
				$sql = "DELETE FROM {$this->tbp}favorite WHERE item_id='{$item_id}' AND type_id='{$type_id}' AND member_id='{$this->member_id}'";
				$this->db->query($sql);
			} else {
				$sql = "INSERT INTO {$this->tbp}favorite(item_id, type_id, member_id, add_time)
					VALUES('{$item_id}', '{$type_id}', '{$this->member_id}', '".time()."')";
				$this->db->query($sql);
			}
		}
		$count = $this->total($item_id, $type_id);
		success($count);
	}

	//我的收藏
	public function index() {
		$type_id = (isset($_GET['type_id']) && intval($_GET['type_id'])) ? intval($_GET['type_id']) : 0;
		$rs = null;
		switch ($type_id) {
			case 1:
				$rs = $this->_goods();
				break;
			case 2:
				$rs = $this->_shops();
				break;
			case 3:
				$rs = $this->_countries();
				break;
			case 4:
				$rs = $this->_articles();
				break;
			default:
				$rs = $this->_goods();
				break;
		}
		success($rs);
	}

	private function _goods() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT g.*
			FROM {$this->tbp}favorite f LEFT JOIN {$this->tbp}goods g ON f.item_id=g.id
			WHERE g.status='1' AND f.member_id='{$this->member_id}' AND f.type_id='1'
			ORDER BY f.id DESC LIMIT {$offset}, {$pagesize}";
		$rs = $this->db->get_results($sql);
		return $rs;
	}

	private function _articles() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT a.*
			FROM {$this->tbp}favorite f LEFT JOIN {$this->tbp}article a ON f.item_id=a.id
			WHERE f.member_id='{$this->member_id}' AND f.type_id='4'
			ORDER BY f.id DESC LIMIT {$offset}, {$pagesize}";
		$rs = $this->db->get_results($sql);
		return $rs;
	}

	private function _countries() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT c.*
			FROM {$this->tbp}favorite f LEFT JOIN {$this->tbp}country c ON f.item_id=c.id
			WHERE f.member_id='{$this->member_id}' AND f.type_id='2'
			ORDER BY f.id DESC LIMIT {$offset}, {$pagesize}";
		$rs = $this->db->get_results($sql);
		return $rs;
	}

	private function _shops() {
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT s.*
			FROM {$this->tbp}favorite f LEFT JOIN {$this->tbp}shop s ON f.item_id=s.id
			WHERE f.member_id='{$this->member_id}' AND f.type_id='2'
			ORDER BY f.id DESC LIMIT {$offset}, {$pagesize}";
		$rs = $this->db->get_results($sql);
		return $rs;
	}
}
